package com.hfjyz.tutor.tutor.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.system.user.bean.UserBean;
import com.hfjyz.tutor.tutor.bean.TutorBean;
import com.hfjyz.tutor.tutor.bean.TutorExcelBean;
import com.hfjyz.tutor.tutor.bean.TutorSearchBean;
import com.hfjyz.tutor.tutor.service.TutorService;

public class TutorAction extends GridBaseAction {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1748589881693831514L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private TutorService service = null;

    private TutorBean tutor = null;
    private String selectIds = null;
    private String tutorId = null;
    private TutorExcelBean tutorDetail = null;

    protected TutorSearchBean tutorSearch = null;

    public TutorService getService() {
        return service;
    }

    public void setService(TutorService service) {
        this.service = service;
    }

    public TutorBean getTutor() {
        return tutor;
    }

    public void setTutor(TutorBean tutor) {
        this.tutor = tutor;
    }

    public TutorExcelBean getTutorDetail() {
        return tutorDetail;
    }

    public void setTutorDetail(TutorExcelBean tutorDetail) {
        this.tutorDetail = tutorDetail;
    }

    public TutorSearchBean getTutorSearch() {
        return tutorSearch;
    }

    public void setTutorSearch(TutorSearchBean tutorSearch) {
        this.tutorSearch = tutorSearch;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public String getTutorId() {
        return tutorId;
    }

    public void setTutorId(String tutorId) {
        this.tutorId = tutorId;
    }

    @Override
    protected List<TutorBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, tutorSearch);

        return service.findTutor(getSearchCondition(), tutorSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getTutorCount(tutorSearch);
    }

    public String save() {
        map = new HashMap<String, Object>();
        try {
            HttpSession session = ServletActionContext.getRequest().getSession();
            UserBean user = (UserBean) session.getAttribute(BaseConstant.SESSION_USER_INFO_KEY);

            service.saveTutor(tutor, user.getId());
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String updateInit() {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            tutor = service.updateInitTutor(id);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() {
        map = new HashMap<String, Object>();
        try {
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteTutor(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String showTutorDetail() throws BaseException {
        Integer id = Integer.valueOf(tutorId);
        tutor = service.updateInitTutor(id);
        tutorDetail = new TutorExcelBean(tutor);
        tutorDetail.decodeChangeLine();
        HttpSession session = ServletActionContext.getRequest().getSession();
        if (tutorDetail.getPhoto() != null && tutorDetail.getPhoto().length > 0) {
            session.setAttribute(BaseConstant.SESSION_USER_PHOTO, tutorDetail.getPhoto());
        } else {
            session.removeAttribute(BaseConstant.SESSION_USER_PHOTO);
        }
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }

    public String exportInit() {
        return SUCCESS;
    }
}
